package com.yilin.tms.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.gson.reflect.TypeToken;
import com.yilin.commons.encrypt.EncryptUtils;
import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.JsonUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;

import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.account.Contact;
import com.yilin.tms.core.entity.user.auth.*;
import com.yilin.tms.core.entity.user.organize.MemberType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.entity.user.organize.UserRole;
import com.yilin.tms.user.mapper.*;
import com.yilin.tms.user.mapper.auth.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.Column;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/migration/")
public class MigrationController extends BaseController {
    @Resource
    AccountMapper accountMapper;
    @Resource
    OrganizeMapper organizeMapper;
    @Resource
    IdCardMapper idCardMapper;
    @Resource
    BusinessLicenseMapper businessLicenseMapper;
    @Resource
    TransBusinessLicenseMapper transBusinessLicenseMapper;
    @Resource
    QualificationCertMapper qualificationCertMapper;
    @Resource
    DrivingLicenseMapper drivingLicenseMapper;
    @Resource
    TaxCertificationMapper taxCertificationMapper;
    @Resource
    StampCompanySealMapper stampCompanySealMapper;
    @Resource
    StampPersonalSignetMapper stampPersonalSignetMapper;
    @Resource
    ContactMapper contactMapper;
    @Resource
    MemberTypeMapper memberTypeMapper;
    @Resource
    RoleMapper roleMapper;

    @PostMapping("memberTypeList")
    @SkipLoginVerify
    public ReturnData<Object> memberTypeList(int page) {
        PageData<MemberType> data = memberTypeMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<MemberType>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("roleList")
    @SkipLoginVerify
    public ReturnData<Object> roleList(int page) {
        PageData<UserRole> data = roleMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<UserRole>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("contactList")
    @SkipLoginVerify
    public ReturnData<Object> contactList(int page) {
        PageData<Contact> data = contactMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<Contact>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }


    @PostMapping("accountList")
    @SkipLoginVerify
    public ReturnData<Object> accountList(int page) {
        PageData<Account> data = accountMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<Account>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {
        });
        for (Map<String, Object> map : dataList) {
            if (map.get("tmsDriverDataState") == null) continue;
            if (map.get("tmsDriverDataState").equals("error")) {
                map.put("tmsDriverDataState", "wait");
            }
            if (map.get("tmsDriverDataState").equals("creating")) {
                map.put("tmsDriverDataState", "wait");
            }
            if (map.get("tmsDriverDataState").equals("success")) {
                map.put("tmsDriverDataState", "complete");
            }
        }
        return jsonView(SUCCESS_CODE, "移植完成", dataList);
    }

    @PostMapping("organizeList")
    @SkipLoginVerify
    public ReturnData<Object> organizeList(int page) {
        PageData<Organize> data = organizeMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<Organize>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {});
        for (Map<String, Object> map : dataList) {
            map.put("superiorPlatId", map.get("superiorPlatOrganizeId"));
            map.put("superiorPlatName", map.get("superiorPlatOrganizeName"));
        }
        return jsonView(SUCCESS_CODE, "移植完成", dataList);
    }

    @PostMapping("idCardList")
    @SkipLoginVerify
    public ReturnData<Object> idCardList(int page) {
        PageData<IdCard> data = idCardMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<IdCard>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("businessLicenseList")
    @SkipLoginVerify
    public ReturnData<Object> businessLicenseList(int page) {
        PageData<BusinessLicense> data = businessLicenseMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<BusinessLicense>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("transBusinessLicenseList")
    @SkipLoginVerify
    public ReturnData<Object> transBusinessLicenseList(int page) {
        PageData<TransBusinessLicense> data = transBusinessLicenseMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TransBusinessLicense>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("qualificationCertList")
    @SkipLoginVerify
    public ReturnData<Object> qualificationCertList(int page) {
        PageData<QualificationCert> data = qualificationCertMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<QualificationCert>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("drivingLicenseList")
    @SkipLoginVerify
    public ReturnData<Object> drivingLicenseList(int page) {
        PageData<DrivingLicense> data = drivingLicenseMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<DrivingLicense>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("taxCertificationList")
    @SkipLoginVerify
    public ReturnData<Object> taxCertificationList(int page) {
        PageData<TaxCertification> data = taxCertificationMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TaxCertification>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {
        });
        for (Map<String, Object> map : dataList) {
            if (map.get("taxInvoiceExpireTime") == null) {
                String signTime = (String) map.get("taxInvoiceSignTime");
                Date date = DateTimeUtil.stringToDate(signTime, null);
                if (date != null) {
                    Date plusDate = DateTimeUtil.plusDate(date, 3, Calendar.YEAR);
                    map.put("taxInvoiceExpireTime", plusDate);
                }
            }
            Organize organize = organizeMapper.selectById((String) map.get("organizeId"));
            IdCard idCard = idCardMapper.selectOne(new QueryWrapper<IdCard>().eq("account_id", organize.getManagerUserId()));
            StampPersonalSignet signet = stampPersonalSignetMapper.selectOne(new QueryWrapper<StampPersonalSignet>().eq("organize_id", organize.getId()));
            map.put("signedPersonName", organize.getManagerUserName());//签订人名称
            map.put("signedPersonPhone", organize.getManagerUserPhone());//签订人电话
            if (idCard != null)
                map.put("signedPersonCardNo", EncryptUtils.aesDecrypt(idCard.getIdCardNo(), null));//签订人证件号
            if (signet != null) map.put("signedPersonSeal", signet.getPersonalSignet());//签订人印章/签名
            if (idCard != null) map.put("signedPersonAddress", idCard.getAddress());//签订人

            Organize plat = organizeMapper.selectById((String) map.get("platId"));
            BusinessLicense businessLicense = businessLicenseMapper.selectOne(new QueryWrapper<BusinessLicense>().eq("organize_id", plat.getId()));
            StampCompanySeal seal = stampCompanySealMapper.selectOne(new QueryWrapper<StampCompanySeal>().eq("organize_id", plat.getId()));
            if (businessLicense != null)
                map.put("platBusinessLicenseNo", EncryptUtils.aesDecrypt(businessLicense.getBusinessNo(), null));//平台营业执照编号
            if (businessLicense != null) map.put("platLegalName", businessLicense.getLegalName());//平台法人名称
            if (businessLicense != null)
                map.put("platLegalCardNo", EncryptUtils.aesDecrypt(businessLicense.getLegalCardNo(), null));//平台
            if (businessLicense != null) map.put("platAddress", businessLicense.getCompanyAddress());//平台营运地址
            if (seal != null) map.put("platSeal", seal.getCompanySeal());//平台印章

        }
        return jsonView(SUCCESS_CODE, "移植完成", dataList);
    }


    @PostMapping("stampCompanySealList")
    @SkipLoginVerify
    public ReturnData<Object> stampCompanySealList(int page) {
        PageData<StampCompanySeal> data = stampCompanySealMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<StampCompanySeal>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }

    @PostMapping("stampPersonalSignetList")
    @SkipLoginVerify
    public ReturnData<Object> stampPersonalSignetList(int page) {
        PageData<StampPersonalSignet> data = stampPersonalSignetMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<StampPersonalSignet>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成", data.getList());
    }


}
